package com.itheima.controller;

import com.itheima.config.AliyunOSSProperties;
import com.itheima.pojo.Result;
import com.itheima.utils.AliyunOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class UploadController {

    @Autowired
    private AliyunOSSProperties aliyunossProperties;

    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile file) throws Exception {

        String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        String url = AliyunOSSUtils.upload(aliyunossProperties.getEndpoint(), aliyunossProperties.getBucketName(), file.getBytes(), extName);
        return Result.success(url);
    }

}
